﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CRM.Component.Common;
using TRPOP.CRM.Component.Business;
using TRPOP.CRM.Component.Model;
using TRPOP.Common;

namespace TRPOP.CRM.Web
{
    /// <summary>
    /// 保存、新增系统角色信息
    /// </summary>
    public partial class SystemRole_save : BaseAdmin
    {
        #region 权限限制
        protected override void SubPageInit()
        {
            base.m_oCurrentPermissionKey = "Employee_SystemRole";
        }
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
                if (RequestHelper.RequestInt("id") > 0)
                {
                    BindData();
                }
            }  
        }

        //修改、新增CompanyCategory
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SystemRoleInfo oSystemRoleInfo;

            if (ViewState["SystemRoleInfo"] != null)
            {
                //修改
                oSystemRoleInfo = ViewState["SystemRoleInfo"] as SystemRoleInfo;
            }
            else
            {
                //新增
                oSystemRoleInfo = new SystemRoleInfo();
                oSystemRoleInfo.dtCreationTime = DateTime.Now;

            }

            oSystemRoleInfo.nvcRoleName = txtRoleName.Text;
            oSystemRoleInfo.nvcDiscription = txtDescription.Text;

            List<int> oListIDs = new List<int>();
            for (int i = 0; i < cbkPermission.Items.Count; i++)
            {
                if (cbkPermission.Items[i].Selected)
                {
                    oListIDs.Add(cbkPermission.Items[i].Value.ToInt());
                }
            }

            //保存
            if (PermissionManage.SaveSysRole(oSystemRoleInfo, oListIDs))
            {
                Alert("保存成功!", "SystemRole_list.aspx");
            }
            else
            {
                Alert("保存失败!");
            }
        }
        #endregion 

        #region 页面方法
        void LoadBase()
        {
            cbkPermission.DataSource = PermissionManage.GetAllPermissionInfo();

            cbkPermission.DataTextField = "nvcName";
            cbkPermission.DataValueField = "iPermissionID";

            cbkPermission.DataBind();
        }
        protected void BindData()
        {
            SystemRoleInfo oSystemRoleInfo = PermissionManage.GetSystemRoleInfoById(RequestHelper.RequestInt("id"));

            if (oSystemRoleInfo != null)
            {
                ViewState["SystemRoleInfo"] = oSystemRoleInfo;
                txtRoleName.Text = oSystemRoleInfo.nvcRoleName;
                this.txtDescription.Text = oSystemRoleInfo.nvcDiscription;
                List<RolePermissionInfo> oList = PermissionManage.GetRolePermissionInfoByRoleID(oSystemRoleInfo.iSystemRoleID);
                if (oList.Count > 0 && cbkPermission.Items.Count > 0)
                {
                    for (int i = 0; i < cbkPermission.Items.Count; i++)
                    {
                        if (oList.Find(oInfo => oInfo.iPermissionID == cbkPermission.Items[i].Value.ToInt()) != null)
                        {
                            cbkPermission.Items[i].Selected = true;
                        }
                    }
                }
            }
        }
        #endregion
    }
}